<script setup lang="ts">
import { onMounted, ref, watch } from "vue";
import { message } from "@/utils/message";
import { FormInstance } from "element-plus";
import { addPlatformDevice, PlatformDevice } from "@/api/LabPlatformApi";
import { Device, listDevices } from "@/api/deviceApi";

const props = defineProps({
  visible: {
    type: Boolean,
    default: false
  },
  platformId: {
    type: String,
    default: "0"
  }
});

const ruleFormRef = ref<FormInstance>();
const allDevices = ref<Array<Device>>([]);

const formVisible = ref(false);
const formData = ref<PlatformDevice>({ ext: {} });
const emit = defineEmits(["update:visible", "add-item"]);
const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  await formEl.validate(async valid => {
    if (valid) {
      formData.value.expId = props.platformId;
      formData.value.ext.name = formData.value.name;
      formData.value.ext.desc = formData.value.desc;
      formData.value.ext = JSON.stringify(formData.value.ext);

      const ret = await addPlatformDevice(formData.value);

      if (ret.errorCode !== 0) {
        message(ret.message, { type: "error" });
      } else {
        message("提交成功", { type: "success" });
        resetForm(formEl);
        formVisible.value = false;
        emit("add-item");
      }
    }
  });
};
const getAllDeviceData = async () => {
  const result = await listDevices();
  allDevices.value = result.data;
};

onMounted(() => {
  getAllDeviceData();
});

const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  formEl.resetFields();
};

const closeDialog = () => {
  formVisible.value = false;
  resetForm(ruleFormRef.value);
};

watch(
  () => formVisible.value,
  val => {
    emit("update:visible", val);
  }
);

watch(
  () => props.visible,
  val => {
    formVisible.value = val;
  }
);

watch(
  () => props.data,
  val => {
    formData.value = val;
  }
);

const rules = {
  name: [{ required: true, message: "请输入名称", trigger: "blur" }],
  deviceId: [{ required: true, message: "请选择控制器", trigger: "blur" }]
};
</script>

<template>
  <el-dialog
    v-model="formVisible"
    title="添加控制器"
    :width="680"
    :before-close="closeDialog"
  >
    <!-- 表单内容 -->
    <el-form
      ref="ruleFormRef"
      :model="formData"
      :rules="rules"
      label-width="100px"
    >
      <el-form-item label="控制器" prop="copyFrom">
        <el-select v-model="formData.deviceId" placeholder="请选择控制器">
          <el-option
            v-for="item in allDevices"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="名称" prop="name">
        <el-input
          v-model="formData.name"
          :style="{ width: '480px' }"
          placeholder="请输入名称"
        />
      </el-form-item>
      <el-form-item label="描述" prop="desc">
        <el-input
          v-model="formData.desc"
          type="textarea"
          :style="{ width: '480px' }"
          placeholder="请输入描述"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="closeDialog">取消</el-button>
      <el-button type="primary" @click="submitForm(ruleFormRef)">
        确定
      </el-button>
    </template>
  </el-dialog>
</template>
